<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <!-- <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="handleSave"
            >新 增</el-button
          >
        </template> -->
    </avue-crud>
  </basic-container>
</template>
  
  <script>
import {
  getList,
  getDetail,
  save,
} from "@/api/rb-merchant/invoiceUserInfo/invoiceUserInfo";
import { mapGetters } from "vuex";

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      search: {},
      active: 0,
      openType: "", //打开dialog弹框的类型
      roleId: null, //角色id
      userType: "3",
      props: {
        //菜单权限数据 props
        label: "title",
        value: "key",
        disabled: this.isDisabledFn,
      },
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        labelWidth: '150',
        columnBtn: false,
        addBtn: true,
        editBtn: true,
        viewBtn: true,
        delBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
            overHidden: true,
          },
          {
            label: "开票人编号",
            prop: "drawerNo",
            type: "input",
            display: false,
            minWidth: 150,
            overHidden: true,
          },
          {
            label: "开票人名称",
            prop: "name",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入开票人名称",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "开票人身份",
            prop: "drawerType",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            // search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=drawer_type",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请选择开票人身份",
                trigger: "blur",
              },
            ],
          },
          {
            label: "证件类型",
            prop: "idType",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            // search: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=id_type",
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "请选择证件类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "证件号码",
            prop: "idCardNo",
            type: "input",
            minWidth: 150,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入证件号码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "是否默认",
            prop: "isDefault",
            type: "radio",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            span: 24,
            value: "1",
            dicData: [
              {
                dictKey: 0,
                dictValue: "否",
              },
              {
                dictKey: 1,
                dictValue: "是",
              },
            ],
            minWidth: 150,
            rules: [
              {
                required: true,
                message: "是否默认",
                trigger: "blur",
              },
            ],
          },
          {
            label: "创建日期",
            prop: "createTime",
            minWidth: 150,
            span: 24,
            addDisplay: false,
            editDisplay: false,
            overHidden: true,
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission","selectMer"]),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  created() {},
  methods: {
    rowSave(row, done, loading) {
      save({...row, rpiNo: this.selectMer.rpiNo}).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      save({...row, rpiNo: this.selectMer.rpiNo}).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    beforeOpen(done, type) {
      this.openType = type;
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    /**
     * @func 菜单权限树形是否禁止
     * @desc dialog打开类型为 view 则禁止权限树
     * @param {String} openType - 打开类型
     * */
    isDisabledFn() {
      const _this = this;
      if (["view"].includes(_this.openType)) {
        return true;
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, {rpiNo: this.selectMer.rpiNo})
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>
  
  <style>
</style>
  